using System;
using System.Collections.Generic;
using System.Linq;

namespace Admin.NET.Application.DL645;

/// <summary>
/// 参数
/// </summary>
public static class DL645ParameterSet
{
	private static List<DL645Parameter> DLparameter = new List<DL645Parameter>
	{
		new DL645Parameter
		{
			Id = 1,
			Key = "3334C335",
			IsRead = true,
			RealKey = "00019002",
			Count = 4,
			Name = "当前剩余电流值",
			RetainCount = 4,
			Company = "mA"
		},
		new DL645Parameter
		{
			Id = 2,
			Key = "33323435",
			IsRead = true,
			RealKey = "00FF0102",
			Count = 4,
			Name = "当前A、B、C三相电压值",
			RetainCount = 3,
			Company = "Kw"
		},
		new DL645Parameter
		{
			Id = 3,
			Key = "3334C435",
			IsRead = true,
			RealKey = "00019102",
			Count = 4,
			Name = "当前额定剩余电流值",
			RetainCount = 4,
			Company = "mA"
		},
		new DL645Parameter
		{
			Id = 4,
			Key = "33323535",
			IsRead = true,
			RealKey = "00FF0202",
			Count = 6,
			Name = "当前A、B、C三相电流",
			RetainCount = 3,
			Company = "A"
		},
		new DL645Parameter
		{
			Id = 5,
			Key = "36373337",
			IsRead = false,
			RealKey = "03040004",
			Count = 64,
			Name = "资产管理编号",
			RetainCount = 64,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 6,
			Key = "35433337",
			IsRead = false,
			RealKey = "02100004",
			Count = 2,
			Name = "控制字2",
			RetainCount = 2,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 7,
			Key = "37433337",
			IsRead = false,
			RealKey = "04100004",
			Count = 2,
			Name = "控制字4",
			RetainCount = 2,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 8,
			Key = "34383337",
			IsRead = false,
			RealKey = "01050004",
			Count = 2,
			Name = "运行状态字1",
			RetainCount = 2,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 9,
			Key = "34473337",
			IsRead = false,
			RealKey = "01140004",
			Count = 6,
			Name = "额定电流整定值过载保护动作电流Ir1",
			RetainCount = 5,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 10,
			Key = "36473337",
			IsRead = false,
			RealKey = "03140004",
			Count = 2,
			Name = "过载保护动作时间t1",
			RetainCount = 2,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 11,
			Key = "36433337",
			IsRead = false,
			RealKey = "03100004",
			Count = 2,
			Name = "控制字3",
			RetainCount = 2,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 12,
			Key = "38473337",
			IsRead = false,
			RealKey = "05140004",
			Count = 4,
			Name = "短路短延时动作时间t2",
			RetainCount = 4,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 13,
			Key = "37473337",
			IsRead = false,
			RealKey = "04140004",
			Count = 2,
			Name = "短路短延时动作电流Ir2 (*Ir1)",
			RetainCount = 2,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 14,
			Key = "39473337",
			IsRead = false,
			RealKey = "06140004",
			Count = 2,
			Name = "短路瞬时电流Ir3 (*Ir1)",
			RetainCount = 2,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 15,
			Key = "34463337",
			IsRead = false,
			RealKey = "01130004",
			Count = 4,
			Name = "过电压整定值",
			RetainCount = 3,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 16,
			Key = "35463337",
			IsRead = false,
			RealKey = "02130004",
			Count = 4,
			Name = "欠电压整定值",
			RetainCount = 3,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 17,
			Key = "36463337",
			IsRead = false,
			RealKey = "03130004",
			Count = 4,
			Name = "断相电压整定值",
			RetainCount = 3,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 18,
			Key = "32463337",
			IsRead = false,
			RealKey = "FF130004",
			Count = 4,
			Name = "电压整定参数块",
			RetainCount = 3,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 19,
			Key = "32473337",
			IsRead = false,
			RealKey = "FF140004",
			Count = 18,
			Name = "电流整定参数块",
			RetainCount = 18,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 20,
			Key = "34433337",
			IsRead = false,
			RealKey = "01100004",
			Count = 2,
			Name = "控制字1",
			RetainCount = 2,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 21,
			Key = "33333333",
			IsRead = false,
			RealKey = "00000000",
			Count = 8,
			Name = "组合有功电能数据块",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 22,
			Key = "33323735",
			IsRead = false,
			RealKey = "00FF0402",
			Count = 6,
			Name = "瞬时无功功率数据块",
			RetainCount = 2,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 23,
			Key = "33323835",
			IsRead = false,
			RealKey = "00FF0502",
			Count = 6,
			Name = "瞬时视在功率数据块",
			RetainCount = 2,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 24,
			Key = "33333633",
			IsRead = false,
			RealKey = "00000300",
			Count = 8,
			Name = "组合无功 1 总电能",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 25,
			Key = "33333433",
			IsRead = false,
			RealKey = "00000100",
			Count = 8,
			Name = "正向有功总电能",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 26,
			Key = "33333533",
			IsRead = false,
			RealKey = "00000200",
			Count = 8,
			Name = "反向有功总电能",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 27,
			Key = "33323635",
			IsRead = false,
			RealKey = "00FF0302",
			Count = 6,
			Name = "瞬时有功功率数据块",
			RetainCount = 2,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 28,
			Key = "39373337",
			IsRead = false,
			RealKey = "06040004",
			Count = 2,
			Name = "最大（壳架）电流(Inm)",
			RetainCount = 2,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 129,
			Key = "33323333",
			IsRead = false,
			RealKey = "00FF0000",
			Count = 8,
			Name = "组合有功电能",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 130,
			Key = "33323433",
			IsRead = false,
			RealKey = "00FF0100",
			Count = 8,
			Name = "正向有功总电能",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 131,
			Key = "33323533",
			IsRead = false,
			RealKey = "00FF0200",
			Count = 8,
			Name = "反向有功电能数据块",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 133,
			Key = "33323935",
			IsRead = false,
			RealKey = "00FF0602",
			Count = 4,
			Name = "总功率因数块",
			RetainCount = 4,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 132,
			Key = "36383337",
			IsRead = false,
			RealKey = "03050004",
			Count = 4,
			Name = "运行状态3",
			RetainCount = 4,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 135,
			Key = "33343333",
			IsRead = false,
			RealKey = "00010000",
			Count = 8,
			Name = "水表645抄表",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 155,
			Key = "34333438",
			IsRead = false,
			RealKey = "01000105",
			Count = 10,
			Name = "瞬时冻结1时间",
			RetainCount = 10,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 165,
			Key = "34343438",
			IsRead = false,
			RealKey = "01010105",
			Count = 8,
			Name = "瞬时冻结1数据",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 255,
			Key = "35333438",
			IsRead = false,
			RealKey = "02000105",
			Count = 10,
			Name = "瞬时冻结1时间",
			RetainCount = 10,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 265,
			Key = "35343438",
			IsRead = false,
			RealKey = "02010105",
			Count = 8,
			Name = "瞬时冻结1数据",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 355,
			Key = "36333438",
			IsRead = false,
			RealKey = "03000105",
			Count = 10,
			Name = "瞬时冻结2时间",
			RetainCount = 10,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 365,
			Key = "36343438",
			IsRead = false,
			RealKey = "03010105",
			Count = 8,
			Name = "瞬时冻结2数据",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 455,
			Key = "37333438",
			IsRead = false,
			RealKey = "04000105",
			Count = 10,
			Name = "瞬时冻结3时间",
			RetainCount = 10,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 465,
			Key = "37343438",
			IsRead = false,
			RealKey = "04010105",
			Count = 8,
			Name = "瞬时冻结3数据",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 555,
			Key = "38333438",
			IsRead = false,
			RealKey = "05000105",
			Count = 10,
			Name = "瞬时冻结4时间",
			RetainCount = 10,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 565,
			Key = "38343438",
			IsRead = false,
			RealKey = "05010105",
			Count = 8,
			Name = "瞬时冻结4数据",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 655,
			Key = "39333438",
			IsRead = false,
			RealKey = "06000105",
			Count = 10,
			Name = "瞬时冻结5时间",
			RetainCount = 10,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 665,
			Key = "39343438",
			IsRead = false,
			RealKey = "06010105",
			Count = 8,
			Name = "瞬时冻结5数据",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 755,
			Key = "3a333438",
			IsRead = false,
			RealKey = "07000105",
			Count = 10,
			Name = "瞬时冻结6时间",
			RetainCount = 10,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 765,
			Key = "3a343438",
			IsRead = false,
			RealKey = "07010105",
			Count = 8,
			Name = "瞬时冻结6数据",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 855,
			Key = "3b333438",
			IsRead = false,
			RealKey = "08000105",
			Count = 10,
			Name = "瞬时冻结7时间",
			RetainCount = 10,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 865,
			Key = "3b343438",
			IsRead = false,
			RealKey = "08010105",
			Count = 8,
			Name = "瞬时冻结7数据",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 955,
			Key = "3c333438",
			IsRead = false,
			RealKey = "09000105",
			Count = 10,
			Name = "瞬时冻结8时间",
			RetainCount = 10,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 965,
			Key = "3c343438",
			IsRead = false,
			RealKey = "09010105",
			Count = 8,
			Name = "瞬时冻结8数据",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 1055,
			Key = "3d333438",
			IsRead = false,
			RealKey = "0a000105",
			Count = 10,
			Name = "瞬时冻结9时间",
			RetainCount = 10,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 1065,
			Key = "3d343438",
			IsRead = false,
			RealKey = "0a010105",
			Count = 8,
			Name = "瞬时冻结9数据",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 1155,
			Key = "3e333438",
			IsRead = false,
			RealKey = "0b000105",
			Count = 10,
			Name = "瞬时冻结10时间",
			RetainCount = 10,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 1165,
			Key = "3e343438",
			IsRead = false,
			RealKey = "0b010105",
			Count = 8,
			Name = "瞬时冻结10数据",
			RetainCount = 6,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 1255,
			Key = "3f333438",
			IsRead = false,
			RealKey = "0c000105",
			Count = 10,
			Name = "瞬时冻结11时间",
			RetainCount = 10,
			Company = ""
		},
		new DL645Parameter
		{
			Id = 1265,
			Key = "3f343438",
			IsRead = false,
			RealKey = "0c010105",
			Count = 8,
			Name = "瞬时冻结11数据",
			RetainCount = 6,
			Company = ""
		}
	};

	/// <summary>
	/// 根据静态字典获取数据
	/// </summary>
	/// <param name="key"></param>
	/// <param name="code">1 额定剩余电流动作值 2电流保护 3极限不驱动时间</param>
	/// <returns></returns>
	public static string GetValueByTypeCode(string key, string code)
	{
		if (1 == 0)
		{
		}
		string result = code switch
		{
			"1" => GetRatedCurrent(key), 
			"2" => GetCurrentProtect(key), 
			"3" => GetLimitTime(key), 
			_ => "", 
		};
		if (1 == 0)
		{
		}
		return result;
	}

	private static string GetLimitTime(string key)
	{
		if (1 == 0)
		{
		}
		string result = key switch
		{
			"00" => "60", 
			"01" => "100", 
			"10" => "200", 
			_ => "", 
		};
		if (1 == 0)
		{
		}
		return result;
	}

	public static string AnalisReading(string infotype, string infoData)
	{
		DL645Parameter parameter = GetDLParameter(infotype);
		if (parameter == null)
		{
			return "";
		}
		Span<string> spandata = new Span<string>(new string[infoData.Length / 2]);
		int i = 0;
		int index = 0;
		while (i < infoData.Length)
		{
			spandata[index] = infoData.Substring(i, 2);
			i += 2;
			index++;
		}
		spandata.Reverse();
		int count = spandata.Length / (parameter.Count / 2);
		Span<string> realinfo = new Span<string>(new string[count]);
		for (i = 0; i < count; i++)
		{
			string value = "";
			for (int j = parameter.Count / 2 * i; j < parameter.Count / 2 * (i + 1); j++)
			{
				value += (Convert.ToInt32(spandata[j], 16) - 51).ToString("X2").PadLeft(2, '0');
			}
			if (parameter.Count != parameter.RetainCount)
			{
				value = value.Insert(parameter.RetainCount, ".");
			}
			realinfo[i] = value;
		}
		return string.Join(',', realinfo.ToArray());
	}

	/// <summary>
	/// 电流保护
	/// </summary>
	/// <param name="key"></param>
	/// <returns></returns>
	/// <exception cref="T:System.NotImplementedException"></exception>
	private static string GetCurrentProtect(string key)
	{
		if (1 == 0)
		{
		}
		string result = key switch
		{
			"00" => "0", 
			"01" => "1", 
			"10" => "2", 
			_ => "", 
		};
		if (1 == 0)
		{
		}
		return result;
	}

	public static string GetCurrentProtectByCode(int code)
	{
		if (1 == 0)
		{
		}
		string result = code switch
		{
			0 => "00", 
			1 => "01", 
			2 => "10", 
			_ => "", 
		};
		if (1 == 0)
		{
		}
		return result;
	}

	private static string GetRatedCurrent(string key)
	{
		if (1 == 0)
		{
		}
		string result = key switch
		{
			"0000" => "100", 
			"0001" => "200", 
			"0010" => "300", 
			"0011" => "400", 
			"0100" => "500", 
			"0101" => "600", 
			"0110" => "800", 
			"0111" => "1000", 
			_ => "", 
		};
		if (1 == 0)
		{
		}
		return result;
	}

	/// <summary>
	/// 返回key字符串
	/// </summary>
	/// <returns></returns>
	public static List<DL645Parameter> GetParameterByKey(string parameter = null)
	{
		if (parameter == null)
		{
			return DLparameter;
		}
		return DLparameter.Where((DL645Parameter u) => u.Key == parameter.ToUpper()).ToList();
	}

	public static string GetParameterById(int Id)
	{
		return DLparameter.Where((DL645Parameter u) => u.Id == Id).FirstOrDefault()?.Key;
	}

	/// <summary>
	/// 返回指定
	/// </summary>
	/// <param name="key"></param>
	/// <returns></returns>
	public static DL645Parameter GetDLParameter(string key)
	{
		return DLparameter.Where((DL645Parameter u) => u.Key == key.ToUpper()).FirstOrDefault();
	}

	/// <summary>
	/// 返回指定
	/// </summary>
	/// <param name="Id"></param>
	/// <returns></returns>
	public static DL645Parameter GetDLParameter(int Id)
	{
		return DLparameter.Where((DL645Parameter u) => u.Id == Id).FirstOrDefault();
	}
}
